package com.derschmale.wick3d.core.io.collada.data
{
	import com.derschmale.wick3d.core.geometry.Vertex3D;
	import com.derschmale.wick3d.core.imagemaps.UVCoords;
	
	public class ColladaGeometry
	{
		public var source : Array;
		
		public var id : String;
		public var name : String;
		
		public var vertices : Array = [];
		public var uvcoords : Array = [];
		public var vertexFaceIndices : Array = [];
		public var uvFaceIndices : Array = [];
		
		public var material : String;
		
		public var numVertices : int;
		public var numTriangles : int;
		
		private var _geometry : XML;
				
		public function ColladaGeometry(geometry : XML)
		{
			id = geometry.@id;
			name = geometry.@name;
			_geometry = geometry;
			
			if (geometry.mesh.hasOwnProperty("triangles")) {
				parseTriangles(geometry.mesh.triangles);
			}
			
			trace ("parsed geometry node, id="+id);
		}
		
		
		private function parseTriangles(triangles : XMLList) : void
		{
			var verticesInput : XMLList = triangles.input.(@semantic=="VERTEX");
			var uvInput : XMLList = triangles.input.(@semantic=="TEXCOORD");
			var verticesOffset : int = verticesInput.@offset;
			var uvOffset : int = uvInput.@offset;
			var triangleIndices : Array = String(triangles.p).split(" ");
			var indexStride : int = triangles.input.length();
			
			numTriangles = triangles.@count;
			
			material = triangles.@material;
			
			parseVertices(String(verticesInput.@source).substr(1));
			parseTexCoords(String(uvInput.@source).substr(1));
			
			for (var i : int = 0; i < triangleIndices.length; i+=indexStride) {
				vertexFaceIndices.push(triangleIndices[i+verticesOffset]);
				uvFaceIndices.push(triangleIndices[i+uvOffset]);
			}
		}
		
		private function parseVertices(inputId : String) : void
		{
			trace ("parsing vertices");
			var source : String = _geometry.mesh.vertices.(@id==inputId).input.(@semantic=="POSITION").@source;
			var sourceNode : XMLList;
			var common : XMLList;
			var arrayId : String;
			var stride : int;
			var floatArray : Array;
			var vertex : Vertex3D;
			
			source = source.substr(1);
			sourceNode = _geometry.mesh.source.(@id==source);
			common = sourceNode.technique_common;
			arrayId = String(common.accessor.@source).substr(1);
			numVertices = common.accessor.@count;
			stride = common.accessor.@stride;
			floatArray = String(sourceNode.float_array.(@id==arrayId)).split(" ");
			
			for (var i : int = 0; i < floatArray.length; i += stride) {
				vertex = new Vertex3D(floatArray[i], floatArray[i+1], floatArray[i+2]);
				vertices.push(vertex);
			}
		}
		
		private function parseTexCoords(inputId : String) : void
		{
			var sourceNode : XMLList;
			var common : XMLList;
			var arrayId : String;
			var stride : int;
			var floatArray : Array;
			var uvcoord : UVCoords;
			
			sourceNode = _geometry.mesh.source.(@id==inputId);
			
			common = sourceNode.technique_common;
			arrayId = String(common.accessor.@source).substr(1);
			numVertices = common.accessor.@count;
			stride = common.accessor.@stride;
			floatArray = String(sourceNode.float_array.(@id==arrayId)).split(" ");
			
			for (var i : int = 0; i < floatArray.length; i += stride) {
				uvcoord = new UVCoords(floatArray[i], 1-floatArray[i+1]);
				uvcoords.push(uvcoord);
			}
		}
	}
}